﻿using System;
using System.IO;
using System.Xml;

namespace Jet.VersionIncrementor
{
	/// <summary>
	/// Base class for parsing .NET project files.
	/// </summary>
	internal abstract class BaseProjectParser
	{
		private readonly XmlNamespaceManager _manager;

		/// <summary>
		/// Root dir for solution
		/// </summary>
		protected string RootDir { get; private set; }

		/// <summary>
		/// .ctor
		/// </summary>
		/// <param name="fileName">Path to project file</param>
		protected BaseProjectParser(string fileName)
		{
			Project = new XmlDocument();
			Project.Load(fileName);
			_manager = new XmlNamespaceManager(Project.NameTable);
			_manager.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003");
			RootDir = Path.GetDirectoryName(fileName);
			ProjectFileName = fileName;
		}

		/// <summary>
		/// Name of current project file
		/// </summary>
		public string ProjectFileName { get; private set; }
		
		/// <summary>
		/// Select nodes according tp xpath
		/// </summary>
		protected XmlNodeList SelectProjectNodes(string xpath)
		{
			return Project.SelectNodes(xpath, _manager);
		}
		
		/// <summary>
		/// Returns current project
		/// </summary>
		protected XmlDocument Project { get; private set; }
		
		/// <summary>
		/// Get or set current version of a project.
		/// </summary>
		public abstract Version CurrentVersion { get; set; }

	}
}
